#include "ElaDialog.h"
#include <QApplication>
#include "ElaApplication.h"
#include "ElaApplication.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QMessageBox>
#include <QTimer>
#include "ElaTheme.h"

class ExampleDialog : public ElaDialog
{
    Q_OBJECT
  public:
    ExampleDialog(QWidget* parent = nullptr) : ElaDialog(parent)
    {
        setWindowTitle("示例对话框");
        resize(400, 300);

        // 设置窗口按钮
        setWindowButtonFlags(ElaAppBarType::StayTopButtonHint | ElaAppBarType::MinimizeButtonHint | ElaAppBarType::MaximizeButtonHint | ElaAppBarType::CloseButtonHint | ElaAppBarType::ThemeChangeButtonHint | ElaAppBarType::NavigationButtonHint);

        // 创建内容
        auto* layout = new QVBoxLayout(this);

        auto* titleLabel = new QLabel("这是一个独立的 ElaDialog 示例");
        titleLabel->setAlignment(Qt::AlignCenter);
        titleLabel->setStyleSheet("font-size: 16px; font-weight: bold; margin: 20px;");

        auto* infoLabel = new QLabel(
            "这个对话框展示了 ElaDialog 的基本功能：\n"
            "• 自定义标题栏\n"
            "• 主题支持\n"
            "• 窗口控制按钮\n"
            "• 居中显示");
        infoLabel->setAlignment(Qt::AlignCenter);

        auto* button = new QPushButton("显示消息");
        button->setFixedSize(120, 35);

        auto* closeButton = new QPushButton("关闭对话框");
        closeButton->setFixedSize(120, 35);

        layout->addWidget(titleLabel);
        layout->addWidget(infoLabel);
        layout->addStretch();
        layout->addWidget(button, 0, Qt::AlignCenter);
        layout->addWidget(closeButton, 0, Qt::AlignCenter);
        layout->addStretch();

        // 连接信号
        connect(button, &QPushButton::clicked, this, []() { QMessageBox::information(nullptr, "提示", "这是来自 ElaDialog 的消息!"); });

        connect(closeButton, &QPushButton::clicked, this, &ExampleDialog::close);

        // 连接标题栏信号
        connect(this, &ExampleDialog::closeButtonClicked, this, &ExampleDialog::close);
        connect(this, &ExampleDialog::themeChangeButtonClicked, this, &ExampleDialog::changeTheme);

        // 居中显示
        QTimer::singleShot(100, this, &ExampleDialog::moveToCenter);
    }

  private Q_SLOTS:
    void changeTheme()
    {
        ElaThemeType::ThemeMode currentMode = eTheme->getThemeMode();
        ElaThemeType::ThemeMode newMode = (currentMode == ElaThemeType::Light) ? ElaThemeType::Dark : ElaThemeType::Light;
        eTheme->setThemeMode(newMode);
    }
};

int main(int argc, char* argv[])
{
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
    QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);  // 启用高 DPI (界面布局和几何)
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
    QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);                                               //  启用自动高 DPI 缩放 (图标和图片)
    QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);  // 使用精确的缩放因子，不进行四舍五入
#else
    // 根据实际屏幕缩放比例更改
    qputenv("QT_SCALE_FACTOR", "1.5");
#endif
#endif

    QApplication app(argc, argv);

    eApp->init();  // 初始化资源、字体等
    // eApp->setWindowDisplayMode(ElaApplicationType::ElaMica);

    ExampleDialog dialog;
    dialog.show();

    return app.exec();
}

#include "main_single_dialog.moc"